const Koa = require("./koa");
const app = new Koa();
// vue next()

function sleep(n) {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, n);
  });
}

// react-redux  compose 实现原理都是一样的
// async await
app.use(async function (ctx, next) {
  console.log(1);
  ctx.body = "1";
  //   throw new Error("出错");
  next(); // 这里没有等待下一个函数执行完毕就直接 响应2了'
  console.log(2);
  ctx.body = "2";
});
app.use(async function (ctx, next) {
  console.log(3);
  ctx.body = "3";
  await sleep(1000);
  await next();
  console.log(4);
  ctx.body = "4";
});
app.use(function (ctx, next) {
  console.log(5);
  ctx.body = "5";
  next();
  console.log(6);
  ctx.body = "6";
});

app.listen(3000, function () {
  console.log(`server start 3000`);
});
app.on("error", function (err) {
  console.log(12, err);
});
